package com.example.yuntiku.Servlet;

import com.example.yuntiku.Service.PagerService;
import com.example.yuntiku.Service.QuestionService;
import com.example.yuntiku.dao.impl.PagerquestionDaoImpl;
import com.example.yuntiku.dao.impl.QuestionDaoImpl;
import com.example.yuntiku.dao.impl.TeacherDao;
import com.example.yuntiku.entity.Pager;
import com.example.yuntiku.entity.PagerQuestion;
import com.example.yuntiku.entity.Question;
import com.example.yuntiku.entity.Teacher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TeacherPagerServlet", value = "/TeacherPagerServlet")
public class TeacherPagerServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Teacher u = (Teacher) request.getSession().getAttribute("user");
        TeacherDao teacherDao=new TeacherDao();
        Teacher teacher=teacherDao.selectById(u.getTid());
        //试卷名称
        String pname=request.getParameter("pname");
        //考试时间
        String duration=request.getParameter("duration");
        //题目总数
        String tmzs = request.getParameter("tmzs");
        //试卷总分
        String fen = request.getParameter("fen");
        //每题分数
        String shufen = request.getParameter("shufen");

        String knowledgetwoString = request.getParameter("knowledgetwoString");
        int cpId=10;
        int cjr=u.getTid();
        int ptype=1;
        int i=0;
        String id="";
        String number="";
        //按分号;分割
        PagerService pagerService=new PagerService();
        Pager pager = new Pager();
        pager.setPname(pname);
        pager.setDuration(duration);
        pager.setTote(Integer.parseInt(tmzs));
        pager.setSumscore(Double.parseDouble(fen));
        pager.setScore(Double.parseDouble(shufen));
        pager.setProductid(cpId);
        pager.setTeacherid(cjr);
        pager.setPtype(ptype);
        i = pagerService.pagerinsert(pager);
        for (String string : knowledgetwoString.split(";")) { // 知识点id,数量
            String[] strings = string.split(",");
            id = strings[0];//知识点id
            number = strings[1];//数量
            PagerquestionDaoImpl pagerquestionDao = new PagerquestionDaoImpl();
            PagerQuestion pagerquestion = new PagerQuestion();
            QuestionDaoImpl questionDao = new QuestionDaoImpl();
            List<Question> list = questionDao.selectQuestion1(id, Integer.parseInt(number));
            System.out.println(list.size());  // 打印返回列表大小，查看是否有数据
            for (Question question : list) {
                pagerquestion.setPagerid(i);
                pagerquestion.setQuestionid(question.getId());
                pagerquestionDao.pagerquestioninsert(pagerquestion);
            }
        }
        request.getRequestDispatcher("PagerServlet").forward(request,response);
    }
}